package com.rayo.accesscontrollercopy.module.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.ActivityManager;
import com.rayo.accesscontrollercopy.app.BaseActivity;
import com.rayo.accesscontrollercopy.model.db.bean.SiteData;
import com.rayo.accesscontrollercopy.model.db.dao.SiteDao;
import com.rayo.accesscontrollercopy.model.db.dao.SiteImpl;
import com.rayo.accesscontrollercopy.module.main.MainActivity;
import com.rayo.accesscontrollercopy.module.setting.SettingActivity;
import com.rayo.accesscontrollercopy.utils.KeyBoardUtils;
import com.rayo.accesscontrollercopy.utils.ToastUtils;


/**
 * Created by ${Adam} on 15/11/11.
 */
public class LoginActivity extends BaseActivity implements ILoginView {
    private LinearLayout mLlLogin;
    private EditText mTvLogin, mTvPwd;
    private Button mBtnLogin,mBtnCancel;

    private LoginPresenter presenter;

    public static final int SETTING = 0;
    public static final int FINISH = 1;
    public static final int OPEN=2;
    public static final String EXTRA_DATA = "com.rayo.login.EXTRA_DATA";

    private int mAction;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void setupViews() {
        mLlLogin = (LinearLayout) findViewById(R.id.ll_login);
        mLlLogin.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.INVISIBLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        mTvLogin = (EditText) findViewById(R.id.tv_login_name);
        mTvPwd = (EditText) findViewById(R.id.tv_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
        mBtnCancel= (Button) findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(this);
    }

    @Override
    protected void initialized() {
        mAction = getIntent().getIntExtra(EXTRA_DATA, FINISH);
        if(mAction==FINISH){
            mBtnLogin.setText(getString(R.string.exit));
        }
        presenter = new LoginPresenter(getContext(), this);
    }

    @Override
    public String getLoginName() {
        return mTvLogin.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mTvPwd.getText().toString().trim();
    }

    @Override
    public void goToOperator() {
        Intent intent = new Intent();
        intent.setClass(this, SettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void goToOpen(String keyId) {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("keyid", keyId);
        startActivity(intent);
    }

    @Override
    public void checkOk() {
        if (SETTING == mAction) {
            goToOperator();
            finish();
        }
        else if(OPEN==mAction){
            goToOpen(presenter.getKeyId());
            finish();
        }
        else if(FINISH==mAction) {
            ActivityManager.getInstance().closeAllActivity();
            System.exit(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:

                if (mTvLogin.getText().toString().equals("*#80*")&&mTvPwd.getText().toString().equals("*#60*")){
                    exitApp();
                }

                SiteDao dao = new SiteImpl();
                SiteData siteData = dao.findFirst();
                if (siteData.getLockid()!=0){
                    if (TextUtils.isEmpty(mTvLogin.getText())||TextUtils.isEmpty(mTvPwd.getText())){
                        ToastUtils.showShortToast(LoginActivity.this,getString(R.string.login_error_null));
                        return;
                    }
                }

                KeyBoardUtils.closeKeybord(mTvLogin,this);
                KeyBoardUtils.closeKeybord(mTvPwd,this);
                int result=presenter.login(mAction);
                if (result==LoginPresenter.OK) {
                    checkOk();
                } else if(result==LoginPresenter.ERROR) {
                    showSnackbar(mLlLogin, R.string.login_error, null);
                }
                else if(result==LoginPresenter.OTHER){
                    showSnackbar(mLlLogin,getString(R.string.must_return_key),null);
                }
                break;
            case R.id.btn_cancel:
                KeyBoardUtils.closeKeybord(mTvLogin,this);
                KeyBoardUtils.closeKeybord(mTvPwd,this);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void exitApp() {
        ActivityManager.getInstance().closeAllActivity();
        System.exit(0);
    }
}
